package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.HyrjReport;
import com.ruoyi.system.service.IHyrjReportService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HyrjAppealMapper;
import com.ruoyi.system.domain.HyrjAppeal;
import com.ruoyi.system.service.IHyrjAppealService;

/**
 * 申诉管理，用于记录和处理用户的申诉Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@Service
public class HyrjAppealServiceImpl implements IHyrjAppealService {
    @Autowired
    private HyrjAppealMapper hyrjAppealMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IHyrjReportService hyrjReportService;

    /**
     * 查询申诉管理，用于记录和处理用户的申诉
     *
     * @param id 申诉管理，用于记录和处理用户的申诉主键
     * @return 申诉管理，用于记录和处理用户的申诉
     */
    @Override
    public HyrjAppeal selectHyrjAppealById(Long id) {
        return hyrjAppealMapper.selectHyrjAppealById(id);
    }

    /**
     * 查询申诉管理，用于记录和处理用户的申诉列表
     *
     * @param hyrjAppeal 申诉管理，用于记录和处理用户的申诉
     * @return 申诉管理，用于记录和处理用户的申诉
     */
    @Override
    public List<HyrjAppeal> selectHyrjAppealList(HyrjAppeal hyrjAppeal) {
        return hyrjAppealMapper.selectHyrjAppealList(hyrjAppeal);
    }

    /**
     * 新增申诉管理，用于记录和处理用户的申诉
     *
     * @param hyrjAppeal 申诉管理，用于记录和处理用户的申诉
     * @return 结果
     */
    @Override
    public int insertHyrjAppeal(HyrjAppeal hyrjAppeal) {
        hyrjAppeal.setAppealTime(new Date());
        hyrjAppeal.setAppealStatus("1");
        HyrjReport hyrjReport = hyrjReportService.selectHyrjReportById(hyrjAppeal.getAppealTargetId());
        if (null != hyrjReport) {
            hyrjReport.setReportStatus("4");
            hyrjReportService.updateHyrjReport(hyrjReport);
        }

        return hyrjAppealMapper.insertHyrjAppeal(hyrjAppeal);
    }

    /**
     * 修改申诉管理，用于记录和处理用户的申诉
     *
     * @param hyrjAppeal 申诉管理，用于记录和处理用户的申诉
     * @return 结果
     */
    @Override
    public int updateHyrjAppeal(HyrjAppeal hyrjAppeal) {
        if (hyrjAppeal.getAppealStatus().equals("2")) {
            SysUser sysUser = userService.selectUserById(hyrjAppeal.getAppealerId());
            if (null != sysUser) {
                Integer reportNumber = null == sysUser.getReportNumber() ? 0 : sysUser.getReportNumber();
                sysUser.setReportNumber(reportNumber - 1);
                userService.updateUser(sysUser);
            }
            HyrjReport hyrjReport = hyrjReportService.selectHyrjReportById(hyrjAppeal.getAppealTargetId());
            if (null != hyrjReport) {
                hyrjReport.setReportStatus("3");
                hyrjReportService.updateHyrjReport(hyrjReport);
            }
        }
        if(hyrjAppeal.getAppealStatus().equals("3")){
            HyrjReport hyrjReport = hyrjReportService.selectHyrjReportById(hyrjAppeal.getAppealTargetId());
            if (null != hyrjReport) {
                hyrjReport.setReportStatus("2");
                hyrjReportService.updateHyrjReport(hyrjReport);
            }
        }
        hyrjAppeal.setAppealTime(new Date());

        return hyrjAppealMapper.updateHyrjAppeal(hyrjAppeal);
    }

    /**
     * 批量删除申诉管理，用于记录和处理用户的申诉
     *
     * @param ids 需要删除的申诉管理，用于记录和处理用户的申诉主键
     * @return 结果
     */
    @Override
    public int deleteHyrjAppealByIds(Long[] ids) {
        return hyrjAppealMapper.deleteHyrjAppealByIds(ids);
    }

    /**
     * 删除申诉管理，用于记录和处理用户的申诉信息
     *
     * @param id 申诉管理，用于记录和处理用户的申诉主键
     * @return 结果
     */
    @Override
    public int deleteHyrjAppealById(Long id) {
        return hyrjAppealMapper.deleteHyrjAppealById(id);
    }
}
